c38fff8128599cb9daecf768cc096f6ad5b68e68
[lhc/web/wiklou.git] / includes / compatability / ctype.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die();
4 /**
5 * PHP <4.2.0 doesn't build ctype_ functions by default and Gentoo doesn't
6 * build it by default, and that probably applies for some other distributions
7 *
8 * These functions should be fully compatable with their PHP equivalents
9 *
10 * @package MediaWiki
11 * @subpackage Compatability
12 *
13 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
14 * @copyright Copyright © 2006, Ævar Arnfjörð Bjarmason
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
16 *
17 * @link http://www.php.net/manual/en/ref.ctype.php PHP Character Type Functions
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 */
33
34 /**#@+
35 * Takes the same input and returns the same values as the equvalent PHP
36 * function
37 *
38 * @param mixed $in
39 * @return bool
40 */
41 function ctype_alnum() {
42 $fname = 'ctype_alnum';
43
44 $args = func_get_args();
45 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
46
47 if ( ! is_null( $ret ) )
48 return $ret;
49 else
50 return (bool)preg_match( '~^[[:alnum:]]+$~i', $in );
51 }
52
53 function ctype_alpha() {
54 $fname = 'ctype_alpha';
55
56 $args = func_get_args();
57 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
58
59 if ( ! is_null( $ret ) )
60 return $ret;
61 else
62 return (bool)preg_match( '~^[[:alpha:]]+$~i', $in );
63 }
64
65 function ctype_cntrl() {
66 $fname = 'ctype_cntrl';
67
68 $args = func_get_args();
69 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
70
71 if ( ! is_null( $ret ) )
72 return $ret;
73 else
74 return (bool)preg_match( '~^[[:cntrl:]]+$~', $in );
75 }
76
77 function ctype_digit() {
78 $fname = 'ctype_digit';
79
80 $args = func_get_args();
81 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
82
83 if ( ! is_null( $ret ) )
84 return $ret;
85 else
86 return (bool)preg_match( '~^[[:digit:]]+$~', $in );
87 }
88
89 function ctype_graph() {
90 $fname = 'ctype_graph';
91
92 $args = func_get_args();
93 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
94
95 if ( ! is_null( $ret ) )
96 return $ret;
97 else
98 return (bool)preg_match( '~^[[:graph:]]+$~', $in );
99 }
100
101 function ctype_lower() {
102 $fname = 'ctype_lower';
103
104 $args = func_get_args();
105 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
106
107 if ( ! is_null( $ret ) )
108 return $ret;
109 else
110 return (bool)preg_match( '~^[[:lower:]]+$~', $in );
111 }
112
113 function ctype_print() {
114 $fname = 'ctype_print';
115
116 $args = func_get_args();
117 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
118
119 if ( ! is_null( $ret ) )
120 return $ret;
121 else
122 return (bool)preg_match( '~^[[:print:]]+$~', $in );
123 }
124
125 function ctype_punct() {
126 $fname = 'ctype_punct';
127
128 $args = func_get_args();
129 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
130
131 if ( ! is_null( $ret ) )
132 return $ret;
133 else
134 return (bool)preg_match( '~^[[:punct:]]+$~', $in );
135 }
136
137 function ctype_space() {
138 $fname = 'ctype_space';
139
140 $args = func_get_args();
141 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
142
143 if ( ! is_null( $ret ) )
144 return $ret;
145 else
146 return (bool)preg_match( '~^[[:space:]]+$~', $in );
147
148 }
149
150
151 function ctype_upper() {
152 $fname = 'ctype_upper';
153
154 $args = func_get_args();
155 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
156
157 if ( ! is_null( $ret ) )
158 return $ret;
159 else
160 return (bool)preg_match( '~^[[:upper:]]+$~', $in );
161
162 }
163
164 function ctype_xdigit() {
165 $fname = 'ctype_xdigit';
166
167 $args = func_get_args();
168 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
169
170 if ( ! is_null( $ret ) )
171 return $ret;
172 else
173 return (bool)preg_match( '~^[[:xdigit:]]+$~i', $in );
174 }
175
176 /**#@-*/
177
178 /**
179 * PHP does some munging on ctype_*() like converting -128 <= x <= -1 to x +=
180 * 256, 0 <= x <= 255 to chr(x) etc, it'll return true if x < -128 and false if
181 * x >= 256, true if the input is an empty string and false if it's of any
182 * other datatype than int or string, this function duplicates that behaviour.
183 *
184 * @param string $fname The name of the caller function
185 * @param array $args The return of the callers func_get_args()
186 * @return array An array with two items, the first is the munged input the
187 * calling function is supposed to use and a return value it
188 * should return if it's not null, $in will be null if $ret is
189 * not null since there's no point in setting it in that case
190 */
191 function wf_ctype_parse_args( $fname, $args ) {
192 $ret = null;
193
194 $cnt = count( $args );
195
196 if ( $cnt !== 1 )
197 trigger_error( "$fname() expects exactly 1 parameter $cnt given", E_USER_WARNING );
198
199 $in = array_pop( $args );
200
201 if ( is_int( $in ) ) {
202 if ( $in >= 256 )
203 return array( null, false );
204 else if ( $in >= 0 )
205 $in = chr( $in );
206 else if ( $in >= -128 )
207 $in = ord( $in + 256 );
208 else if ( $in < -128 )
209 return array( null, true );
210 }
211
212 if ( is_string( $in ) )
213 if ( $in === '' )
214 return array( null, true );
215 else
216 return array( $in, null );
217 else
218 // Like PHP
219 return array( null, false );
220 }